package com.bona.service.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bona.dao.common.BaseDao;
import com.bona.dao.system.SysCommonDao;
import com.bona.dao.system.SysRoleDao;
import com.bona.dao.system.SysRoleUserDao;
import com.bona.dao.system.SysUserDao;
import com.bona.dao.system.SysUserOrgDao;
import com.bona.model.system.SysRoleUserModel;
import com.bona.model.system.SysUserModel;
import com.bona.model.system.SysUserOrgModel;
import com.bona.service.common.BaseService;
import com.bona.util.common.UUIDGenerator;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * 
 * <br>
 * <b>功能：</b>SysUserService<br>
 * <b>作者：</b>tbzeng<br>
 * <b>日期：</b> Dec 9, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，tbzeng<br>
 */
@Service("userService")
@Transactional
public class SysUserService extends BaseService {
	
	private final static Logger logger = Logger.getLogger(SysUserService.class);
	
	@Autowired
	private SysUserDao userDao;
	@Autowired
	private SysCommonDao commonDao;
	@Autowired
	private SysRoleDao roleDao;
	@Autowired
	private SysUserOrgDao userOrgDao;
	@Autowired
	private SysRoleUserDao roleUserDao;
	
	/**
	 * 获取用户所在机构
	 * @param userId
	 * @return
	 */
	public List<String> queryOrgIdsByUserId(String userId) {
		
		List<String> orgIdlist = new ArrayList<String>();
		SysUserOrgModel model = new SysUserOrgModel();
		model.setUserId(userId);
		List<SysUserOrgModel> userOrgList = userOrgDao.queryByPage(model, new PageBounds());
		if (userOrgList != null) {
			for (SysUserOrgModel userOrg: userOrgList) {
				orgIdlist.add(userOrg.getId());
			}
		}
		return orgIdlist;
	}
	
	/**
	 * 获取部门下的用户id
	 * @param orgId
	 * @return
	 */
	public List<String> queryUserIdsByOrgId(String orgId) {
		
		List<String> userIdlist = new ArrayList<String>();
		SysUserOrgModel model = new SysUserOrgModel();
		model.setOrgId(orgId);
		List<SysUserOrgModel> userOrgList = userOrgDao.queryByPage(model, new PageBounds());
		if (userOrgList != null) {
			for (SysUserOrgModel userOrg: userOrgList) {
				userIdlist.add(userOrg.getId());
			}
		}
		return userIdlist;
	}
	
	/**
	 * 删除用户关联的角色
	 */
	public Integer deleteRoleUserByUserId(String userId) {
		
		SysRoleUserModel roleUser = new SysRoleUserModel();
		roleUser.setUserid(userId);
		return roleUserDao.delete(roleUser);
	}
	
	/**
	 * 删除用户关联的机构信息
	 * @param userId
	 */
	public Integer deleteUserOrgByUserId(String userId) {
		
		SysUserOrgModel userOrg = new SysUserOrgModel();
		userOrg.setUserId(userId);
		return userOrgDao.delete(userOrg);
	}
	
	/**
	 * 查询用户所关联的角色编号
	 * @param userId
	 * @return
	 */
	public List<String> queryRoleIdsByUserId(String userId) {
		
		List<String> roleIds = userDao.queryRoleIdsByUserId(userId);
		return roleIds;
	}
	
	/**
	 * 设置用户机构关联关系
	 * @param models
	 */
	public void addUserToOrg(String departId, String userIds) {
		
		if (StringUtils.isBlank(departId) || StringUtils.isBlank(userIds)) return;
		for (String userId: userIds.split(",")) {
			SysUserOrgModel userOrgModel = new SysUserOrgModel();
			userOrgModel.setId(UUIDGenerator.generate());
			userOrgModel.setUserId(userId);
			userOrgModel.setOrgId(departId);
			userOrgDao.add(userOrgModel);
		}
	}
	
	/**
	 * 添加用户，事务控制
	 * @param user
	 * @param roleIds
	 * @param orgIds
	 */
	public void add(SysUserModel user, String roleIds, String orgIds) {
		
		String userId = user.getId();
		if (StringUtils.isBlank(userId)) {	// 新增用户信息
			user.setId(UUIDGenerator.generate());
			add(user);
		} else {
			// 更新用户信息
			SysUserModel model = new SysUserModel();
			model.setId(user.getId());
			model = userDao.query(model);
			model.setUsername(user.getUsername());
			model.setRealname(user.getRealname());
			model.setEmail(user.getEmail());
			model.setMobilephone(user.getMobilephone());
			model.setOfficephone(user.getOfficephone());
			model.setUpdateBy(user.getUpdateBy());
			model.setUpdateName(user.getUpdateName());
			model.setUpdateDate(new Date());
			userDao.update(model);
			// 删除关联机构
			SysUserOrgModel userOrg = new SysUserOrgModel();
			userOrg.setUserId(userId);
			userOrgDao.delete(userOrg);
			// 删除关联角色
			SysRoleUserModel roleUser = new SysRoleUserModel();
			roleUser.setUserid(userId);
			roleUserDao.delete(roleUser);
		}
		// 新增关联机构
		String[] orgIdArr = orgIds.split(",");
		for (String orgId: orgIdArr) {
			SysUserOrgModel userOrgModel = new SysUserOrgModel();
			userOrgModel.setId(UUIDGenerator.generate());
			userOrgModel.setUserId(user.getId());
			userOrgModel.setOrgId(orgId);
			userOrgDao.add(userOrgModel);
		}
		// 新增关联角色
		String[] roleIdArr = roleIds.split(",");
		for (String roleId: roleIdArr) {
			SysRoleUserModel roleUserModel = new SysRoleUserModel();
			roleUserModel.setId(UUIDGenerator.generate());
			roleUserModel.setUserid(user.getId());
			roleUserModel.setRoleid(roleId);
			roleUserDao.add(roleUserModel);
		}
	}
	
	/**
	 * 删除用户 事物控制
	 * @param userId
	 */
	public void deleteUser(String userId) {
		
		//1、 删除用户关联的角色
		SysRoleUserModel roleUser = new SysRoleUserModel();
		roleUser.setUserid(userId);
		int roleUserCnt = roleUserDao.delete(roleUser);
		//2、删除用户关联的机构
		SysUserOrgModel userOrg = new SysUserOrgModel();
		userOrg.setUserId(userId);
		int userOrgCnt = userOrgDao.delete(userOrg);
		//3、删除用户信息
		int userCnt = userDao.delete(new SysUserModel(userId));
		logger.info("删除用户关联角色["+roleUserCnt+"],用户关联机构["+userOrgCnt+"],用户信息["+userCnt+"]");
	}
	
	@Override
	public BaseDao getDao() {
		
		return userDao;
	}

}
